package com.bana.sys.controller;

import com.bana.sys.code.DatabaseResult;
import com.bana.sys.dao.*;
import com.bana.sys.model.*;
import com.bana.sys.service.RoleService;
import com.bana.sys.utils.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/role",method = RequestMethod.POST)
public class RoleController {

    @Resource
    private RoleDAO roleDAO;

    @Resource
    private RoleService roleService;

    /**
     * 查询角色
     * @param request jsmc 角色名称，分页
     * @return
     */
    @RequestMapping(value = "/getRoles.do", method = RequestMethod.POST)
    public PageResponse<Role> getRoles(@RequestBody TokenRequest<RolePageParameter> request) {


        RolePageParameter parameter=request.getData();
        List<Role> roles=roleService.getRoles(parameter);
        int count=roleDAO.getRoleCount(parameter);
        return new PageResponse<>(roles,count);
    }


    /**
     * 新增角色
     * @param request 角色
     * @return
     */
    @RequestMapping(value = "/insertRole.do", method = RequestMethod.POST)
    public ActionResult insertRole(@RequestBody TokenRequest<Role> request) {

        ActionResult actionResult=new ActionResult();
        roleService.insertRole(request.getData());
        actionResult.setKey(request.getData().getJsbm());
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

    /**
     * 更新角色
     * @param request 角色
     * @return
     */
    @RequestMapping(value = "/updateRole.do", method = RequestMethod.POST)
    public ActionResult updateRole(@RequestBody TokenRequest<Role> request) {

        ActionResult actionResult=new ActionResult();
        roleService.updateRole(request.getData());
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

    /**
     * 删除角色集合及关联信息
     * @param request jsbm,角色编码
     * @return
     */
    @RequestMapping(value = "/deleteRole.do", method = RequestMethod.POST)
        public ActionResult deleteRole(@RequestBody TokenRequest<Role> request) {

        roleService.deleteRole(request.getData().getJsbm());
        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(DatabaseResult.Success);
        return actionResult;
    }

}
